package com.screen.screendisplay.service.impl.electric;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.screen.screendisplay.entity.electric.ElectricProdoperFuelFrom;
import com.screen.screendisplay.mapper.electric.ElectricProdoperFuelFromMapper;
import com.screen.screendisplay.service.electric.ElectricProdoperFuelFromService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.screen.screendisplay.vo.electric.CoalSourceDistribution;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 燃料来源表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class ElectricProdoperFuelFromServiceImpl extends ServiceImpl<ElectricProdoperFuelFromMapper, ElectricProdoperFuelFrom> implements ElectricProdoperFuelFromService {

    /**
     * 煤炭来源分布情况
     * @param year
     * @return
     */
    @Override
    public List<ElectricProdoperFuelFrom> distributionCoalSources(Integer year) {
        LambdaQueryWrapper<ElectricProdoperFuelFrom> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ElectricProdoperFuelFrom::getYear, year);
        return this.list(lqw);
    }

    /**
     * 获取每个电厂的煤炭来源分布情况
     * @param year 年份
     * @return 煤炭来源分布列表
     */
    public List<CoalSourceDistribution> getCoalSourceDistributionByPlant(Integer year) {
        // 查询原始数据
        List<ElectricProdoperFuelFrom> rawDataList = this.distributionCoalSources(year);
        
        // 按电厂分组数据
        List<CoalSourceDistribution> result = new ArrayList<>();
        
        // 获取所有不同的电厂编码
        List<String> powerPlantCodes = rawDataList.stream()
                .map(ElectricProdoperFuelFrom::getPowerPlantCode)
                .distinct()
                .collect(Collectors.toList());
        
        // 为每个电厂构建CoalSourceDistribution对象
        for (String powerPlantCode : powerPlantCodes) {
            CoalSourceDistribution distribution = new CoalSourceDistribution();
            distribution.setYear(year);
            
            // 获取该电厂的所有数据
            List<ElectricProdoperFuelFrom> plantDataList = rawDataList.stream()
                    .filter(data -> powerPlantCode.equals(data.getPowerPlantCode()))
                    .collect(Collectors.toList());
            
            // 设置电厂名称（从任意一条记录中获取）
            if (!plantDataList.isEmpty()) {
                distribution.setPowerPlantCode(powerPlantCode);
                distribution.setPowerPlantName(plantDataList.get(0).getPowerPlantName());
            }
            
            // 根据coalMineType设置不同的煤矿来源量
            for (ElectricProdoperFuelFrom fuelFrom : plantDataList) {
                Byte coalMineType = fuelFrom.getCoalMineType();
                switch (coalMineType) {
                    case 0: // 南矿
                        distribution.setSouthMineAmount(fuelFrom.getCoalSourceAmount());
                        break;
                    case 1: // 北矿
                        distribution.setNorthMineAmount(fuelFrom.getCoalSourceAmount());
                        break;
                    case 2: // 扎矿
                        distribution.setZaMineAmount(fuelFrom.getCoalSourceAmount());
                        break;
                    case 3: // 二号矿
                        distribution.setNo2MineAmount(fuelFrom.getCoalSourceAmount());
                        break;
                    case 4: // 三号矿
                        distribution.setNo3MineAmount(fuelFrom.getCoalSourceAmount());
                        break;
                }
            }
            
            result.add(distribution);
        }
        
        return result;
    }
}